import { Component, ViewChild, OnInit } from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import { CarService } from '../car.service';
import { HttpService } from '../http.service';
import { PayService } from '../pay.service';
import { Router } from '@angular/router';
import * as ons from 'onsenui';



@Component({
  selector: 'app-car-info',
  templateUrl: './car-info.component.html',
  styleUrls: ['./car-info.component.less']
})

export class CarInfoComponent implements OnInit {
  @ViewChild('dialog') dialog;

  list=[];
  id;
  loading=true;
  uploading=false;

  selected=require('../../assets/img/selected.png');
  await=require('../../assets/img/await.png');
  forbid=require('../../assets/img/forbid.png');
  empty=require('../../assets/img/empty.png');


  constructor(private router: Router,
    private route: ActivatedRoute,
    private carService: CarService,
    private http: HttpService,
    private pay: PayService,
  ) {
    const id = this.route.snapshot.queryParams['id'];
    this.id = id;
    this.carService.carInfo(id).then(res => {
      this.loading = false;
      if(res.success) this.list = res.data.violation;
      else ons.notification.toast(res.msg, {timeout: 2000});
    })
    this.carService.isBindId(id).then(res => {
      if(res.data.is_drivingLicense == 0) this.isBindId = false;
      else this.isBindId = true;
    })
    this.pay.getJsSDK();
  }

  isBindId: Boolean

  get validatedList() {
    return this.list.filter(l => l.violation_status == 0)
  }

  get totalCost() {
    return this.validatedList.reduce((p: any, c) => p+Number(c.lateFee)+Number(c.serviceFee)+Number(c.fine), 0)
  }

  get totalPoint() {
    return this.validatedList.reduce((p: any, c) => p+Number(c.point), 0)
  }

  toBindId(){
    this.router.navigate([`bindId/${this.id}`]);
  }


  async cashPay(e){
    this.uploading = true;
    const fm = new FormData();
    fm.append('name', 'car.upload.img');
    fm.append('img', e.target.files[0]);
    const {data} = await this.http.form(fm);
    await this.http.post({
      name:'car.offline.pay',
      pay_pic: data.original,
      violation_id: this.validatedList.map(e=>e.violation_id).join()
    })
    this.dialog.nativeElement.hide();
    // 重置拉取订单部分
    this.carService.order.requestSent = false;
    this.router.navigate(['orderList'], {replaceUrl: true});
  }

  async wxPay(){
    const {data} = await this.http.post({
      name:'car.wx.pay',
      violation_id: this.validatedList.map(e=>e.violation_id).join()
    });
    try {
      await this.pay.pay({
        timestamp: data.pay_need.timestamp_pay,
        nonceStr: data.pay_need.nonceStr_pay,
        package_wx: `prepay_id=${data.wx_return.prepay_id}`,
        signType: 'MD5',
        paySign: data.pay_need.sign_pay
      });
      this.carService.order.requestSent = false;
      this.router.navigate(['orderList',{replaceUrl: true}]);
    } catch(err) {
      ons.notification.toast('支付失败或已取消!', {timeout: 2000});
    }
  }


  ngOnInit() {
  }

}
